<template>
  <div class="app-container">
    <el-row :gutter="20">
      <!--工作区数据-->
      <el-col :span="4" :xs="24">
        <div class="head-container">
          <el-input
            v-model="workspaceName"
            placeholder="请输入名称"
            clearable
            size="small"
            prefix-icon="el-icon-search"
            style="margin-bottom: 20px"
          />
        </div>
        <div class="head-container">
          <el-tree
            :data="workspaceOptions"
            :props="defaultProps"
            :expand-on-click-node="false"
            :filter-node-method="filterNode"
            ref="tree"
            default-expand-all
            @node-click="handleNodeClick"
          />
        </div>
      </el-col>
      <!--数据-->
      <el-col :span="20" :xs="24">
        <el-table v-loading="loading" :data="fileList" stripe @selection-change="handleSelectionChange" :header-cell-style="{'background': '#F6F8FB'}" >
          <el-table-column type="selection" width="50" align="center" />
          <el-table-column label="编号" align="center" prop="name" />
          <el-table-column label="名称" align="center" prop="name" :show-overflow-tooltip="true" />
          <el-table-column label="标题" align="center" prop="title" :show-overflow-tooltip="true" />
          <el-table-column label="状态" align="center" prop="current"/>
          <el-table-column label="策略" align="center" prop="policy" :show-overflow-tooltip="true" />
          <el-table-column label="类型" align="center" prop="type" :show-overflow-tooltip="true" />
          <el-table-column label="文件名" align="center" prop="filename" :show-overflow-tooltip="true" />
          <el-table-column
            label="操作"
            align="center"
            width="150"
            class-name="small-padding fixed-width"
          >
            <template slot-scope="scope">
              <el-button
                v-if="scope.row.id !== 1"
                size="mini"
                type="text"
                icon="el-icon-delete"
                @click=""
                v-hasPermi="['system:user:remove']"
              >删除</el-button>
            </template>
          </el-table-column>
        </el-table>

        <pagination
          v-show="total>0"
          :total="total"
          :page.sync="queryFileParams.pageNum"
          :limit.sync="queryFileParams.pageSize"
          @pagination="getFileList"
        />
      </el-col>
    </el-row>

  </div>
</template>

<script>
import { detailList,fileList } from "@/api/pms/workspace";
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";

export default {
  name: "Workspace_detail",
  components: { Treeselect },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 总条数
      total: 0,
      // 表格数据
      dataList: null,
      fileList: null,
      // 弹出层标题
      title: "",
      // 树选项
      workspaceOptions: undefined,
      // 是否显示弹出层
      open: false,
      // 名称
      workspaceName: undefined,
      // 表单参数
      form: {},
      defaultProps: {
        children: "children",
        label: "label"
      },
      // 查询参数
      queryParams: {
        objectId: undefined
      },
      queryFileParams:{
        pageNum: 1,
        pageSize: 10,
        objectId: undefined
      }
    };
  },
  watch: {
    // 根据名称筛选部门树
    workspaceName(val) {
      this.$refs.tree.filter(val);
    }
  },
  created() {
    const objectId = this.$route.params && this.$route.params.objectId;
    this.queryParams.objectId = objectId;
    this.getList();
  },
  methods: {
    /** 查询工作区 */
    getList() {
      this.loading = true;
      detailList(this.queryParams).then(response => {
          response.rows.push({id:this.queryParams.objectId,parentId:0,name:"工作区"});
          response.rows.forEach((item,index)=>{
            response.rows[index]=this.normalizer(item);
          });
          this.dataList = this.handleTree(response.rows, "id");
          this.workspaceOptions=this.dataList;
          this.loading = false;
        }
      );
    },
    getFileList() {
      this.loading = true;
      fileList(this.queryFileParams).then(response => {
          this.fileList = response.rows;
          this.total = response.total;
          this.loading = false;
        }
      );
    },
    /** 转换数据结构 */
    normalizer(node) {
      return {
        id: node.id,
        label: node.name,
        parentId: node.parentId
      };
    },
    // 筛选节点
    filterNode(value, data) {
      if (!value) return true;
      return data.label.indexOf(value) !== -1;
    },
    // 节点单击事件
    handleNodeClick(data) {
      this.queryFileParams.objectId = data.id;
      this.getFileList();
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {

      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.page = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.userId);
      this.single = selection.length != 1;
      this.multiple = !selection.length;
    }
  }
};
</script>
